package com.woniu.mcpservice.tools;

import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.internalcommon.remote.ServiceMapClient;
import com.woniu.internalcommon.request.POIRequestDTO;
import com.woniu.internalcommon.response.POIResponse;
import com.woniu.internalcommon.response.WeatherLiveResponse;
import com.woniu.internalcommon.util.TimeUtils;
import lombok.Data;
import lombok.extern.java.Log;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Component
public class MapServiceTools {
    @Autowired
    private ServiceMapClient serviceMapClient;

    @Tool(description = "POI周边搜索工具")
    public String getPOIMessage(@ToolParam(description = "用户当前的坐标 pos") String location,
                                @ToolParam (description = "POI类型") String type,
                                @ToolParam (description = "radius default 5000") Integer radius){

//        "39.990475,116.481499"
        System.out.println(location);
        System.out.println(type);
        String[] split = type.split("\\|");
        List<String> types = Arrays.stream(split).toList();
        System.out.println("---"+types);
        List<POIResponse> data = serviceMapClient.aroundPOISearch(new POIRequestDTO(location
                , radius, types)).getData();
        System.out.println(data);
        StringBuffer stringBuffer = new StringBuffer();
        for (POIResponse poiR: data){
            stringBuffer.append("name: ").append(poiR.getName()).append("\n")
                    .append("address: ").append(poiR.getAddress()).append("\n")
                    .append("distance: ").append(poiR.getDistance()).append("\n")
                    .append("location: ").append(poiR.getLocation()).append("\n")
                    .append("rating: ").append(poiR.getBusiness().getRating()).append("\n")
                    .append("type: ").append(poiR.getType()).append("\n")
                    .append("photo: ").append(poiR.getPhotos()).append("\n");

            System.out.println(stringBuffer);
        }


        return stringBuffer.toString();
    }
    @Tool (description = "当前天气查询工具")
    public String getWeather(@ToolParam(description = "城市编码 citycode") String city){
        List<?> data = serviceMapClient.getLiveWeather(city).getData();
        System.out.println(data);
        StringBuffer stringBuffer = new StringBuffer();
        for (Object  weatherR: data) {
            System.out.println(weatherR);
            WeatherLiveResponse weather = (WeatherLiveResponse) weatherR;
            stringBuffer.append(" 天气: ").append(weather.getWeather()).append("\n")
                    .append("  温度: ").append(weather.getTemperature()).append("\n")
                    .append("  风向: ").append(weather.getWindDirection()).append("\n")
                    .append("  风级: ").append(weather.getWindPower()).append("\n");
        }
        System.out.println(stringBuffer);
        return stringBuffer.toString();
    }
//    @Tool (description = "未来天气查询")
//    public String getWeather(@ToolParam()  ){
//        serviceMapClient.getForWeather("上海");
//
//        return "11";
//    }

    /**
     * 获取当前时间
     * @param timeZoneId
     * @return
     */
    @Tool(description = "获取当前时间")
    public String  getCityTimeMethod(@ToolParam(description = "Time zone id, such as Asia/Shanghai ") String timeZoneId) {
        System.out.println("The current time zone is {}"+ timeZoneId);
        timeZoneId = "Shanghai";
        return String.format("The current time zone is %s and the current time is " + "%s", timeZoneId,
                TimeUtils.getTimeByZoneId(timeZoneId));

    }

    @Tool (description = "查询所有POI类型关键字")
    public String getAllPOITypeNames() {
        ResponseResult<List<String>> responseResult = serviceMapClient.getAllPOITypeNames();
        List<String> data = responseResult.getData();
        StringBuilder result = new StringBuilder();
        data.forEach(s -> result.append(s).append("，"));

        return result.toString();
    }
}
